package September20;

public class StringBuilderVsStringBuffer {
    public static void main(String[] args) {
        final int COUNT = 100000;
        String testString = "Hello, World!";

        // 测试StringBuilder
        long startTime = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < COUNT; i++) {
            stringBuilder.append(testString);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("StringBuilder time: " + (endTime - startTime) + " ms");

        // 测试StringBuffer
        startTime = System.currentTimeMillis();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < COUNT; i++) {
            stringBuffer.append(testString);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer time: " + (endTime - startTime) + " ms");
    }
}
